#include "Class.h"

int Date::GetMonthDay(int year, int month)
{
	int Month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return Month[month];
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day=day;
}

Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

Date::~Date()
{
	_year = _month = _day = 0;
}

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (GetMonthDay(_year, _month) < _day)
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
	Date temp(*this);
	temp += day;
	return temp;
}

Date Date::operator-(int day)
{
	Date temp(*this);
	temp -= day;
	return temp;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	while (day > GetMonthDay(_year, _month))
	{
		day -= GetMonthDay(_year, _month);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	_day -= day;
	if (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day = GetMonthDay(_year, _month) + _day;
	}
	return *this;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date Date::operator++(int)
{
	Date temp(*this);
	*this+= 1;
	return temp;
}

Date Date::operator--(int)
{
	Date temp(*this);
	if ((_day - 1) <0)
	{
		_day = GetMonthDay(_year,_month);
		_month--;
		if (_month == -1)
		{
			_month = 12;
			_year--;
		}
	}
	return temp;
}

Date& Date::operator--()
{
	if ((_day - 1) < 0)
	{
		_day = GetMonthDay(_year, _month);
		_month--;
		if (_month == -1)
		{
			_month = 12;
			_year--;
		}
	}
	return *this;
}

bool Date::operator>(const Date& d)
{
	if (_year > d._year || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day))
	{
		return true;
	}
	return false;
}

bool Date::operator==(const Date& d)
{
	if (_year == d._year && _month == d._month && _day == d._day)
	{
		return true;
	}
	return false;
}

bool Date::operator>=(const Date& d)
{
	return (*this>d || *this==d);
}

bool Date::operator<(const Date& d)
{
	return !(*this>=d);
}

bool Date::operator<=(const Date& d)
{
	return (*this < d || *this == d);
}

bool Date::operator!=(const Date& d)
{
	return !(*this==d);
}
int YearDay(int year)
{
	int day = 0;
	for (int i = 1; i <= 12; i++)
	{
		day += Date::GetMonthDay(year, i);
	}
	return day;
}
int Date::operator-(const Date& d)
{
	int day = 0;
	Date min = *this;
	Date max = d;
	if (*this < d)
	{
		while (min < d)
		{
			day++;
			++min;
		}
	}
	else
	{
		while (max < *this)
		{
			day++;
			++max;
		}
	}
	return day;
}








